package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;

import com.bbx.api.sdk.model.official.passanger.Officail_UnprocessedBuild;
import com.bbx.api.sdk.model.official.passanger.Official_OrderListBuild;
import com.bbx.api.sdk.model.official.passanger.Official_OrderListBuild.*;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderList;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrders;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrders2;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.UnprocessedList.Officail_Unprocessed;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.UnprocessedList.Officail_Unprocessed2;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.UnprocessedList.UnprocessedList;
import com.bbx.api.sdk.model.passanger.MyOrder;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_OrderListNet;
import com.bbx.api.sdk.net.official.passanger.Officail_UnprocessedNet;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Util.OrderListConvert;
import com.bbx.taxi.client.Util.UnprocessedListConvert;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Menu.MyOrderActivity;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.UnprocessedList.UnprocessedListDB;

/** 
 * @Path com.bbx.taxi.client.Task.MyUnprocessed.java
 * @Author 严旭文
 * @Date 2016-1-28 上午9:54:55
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 待审核跟审核记录
 */
public class MyUnprocessedTask {
	/**待审核*/
	public final static int STATUS_UNPROCESSED=1;
	/**已审核记录*/
	public final static int STATUS_PROCESSED=6;
	//
	public final static int STATUS_SUCCESS=0;
	public final static int STATUS_NULL=1;
	public final static int STATUS_FAil=-1;
	private Context context;
	public static MyUnprocessedTask instance;
	public UnprocessedTask mUnprocessedTask;
	private UnprocessedListDB db_unprocessed;// 我的订单数据库
	private ArrayList<UnprocessedList> list;
	int type;
	public interface OnUnprocessedFinishListener {
		/**
		 * @param status 0成功
		 * @param list
		 * @param allcount 总个数，用于判断是否继续加载
		 */
		public void onUnprocessedFinish(int status,ArrayList<UnprocessedList> list,int allcount);
	}
	static OnUnprocessedFinishListener mOnUnprocessedFinishListener ;

	public static void setOnOrderFinishListener(OnUnprocessedFinishListener l) {
		mOnUnprocessedFinishListener=l;
	}
	public static void removeOnOrderFinishListener(OnUnprocessedFinishListener l) {
		mOnUnprocessedFinishListener=null;
	}
	public static MyUnprocessedTask getInstance(Context context,int count,int position,int orderstate) {
		instance = new MyUnprocessedTask(context,count,position,orderstate);
		return instance;
	}
	public MyUnprocessedTask(Context context,int count,int position,int orderstate) {
		this.context = context;
		db_unprocessed = new UnprocessedListDB(context);
		getUnprocessed(count,position,orderstate);
	}
	/**
	 * 获取未审核订单
	 */
	public void getUnprocessed(int count,int position,int orderstate){
		list=null;
		Officail_UnprocessedBuild mOfficail_UnprocessedBuild = new Officail_UnprocessedBuild(context);
		mOfficail_UnprocessedBuild.opuid =MyApplication.getInstance().getUid();
		mOfficail_UnprocessedBuild.targetuid =MyApplication.getInstance().getUid();
		mOfficail_UnprocessedBuild.orderstate=orderstate;
		mOfficail_UnprocessedBuild.pos = position;
		mOfficail_UnprocessedBuild.limit = count;
		mUnprocessedTask = new UnprocessedTask(context, mOfficail_UnprocessedBuild);
		mUnprocessedTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 进行中的订单
	 */
	private class UnprocessedTask extends BaseAsyncTask {

		private Officail_UnprocessedBuild mOfficail_UnprocessedBuild;
		public UnprocessedTask(Context context, Officail_UnprocessedBuild mOfficail_UnprocessedBuild) {
			super(context,false,0);
			this.mOfficail_UnprocessedBuild = mOfficail_UnprocessedBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				try{
					Officail_Unprocessed mOfficail_Unprocessed = (Officail_Unprocessed) object;
					list=mOfficail_Unprocessed.getList();
				}
				catch(Exception e){
					Officail_Unprocessed2 mOfficail_Unprocessed2 = (Officail_Unprocessed2) object;
					list=UnprocessedListConvert.onConvert(mOfficail_Unprocessed2.getList());
				}
				
				if (list!= null && !list.isEmpty()) {
					// 存储数据库
					db_unprocessed.onWriteDB(context,list,true,null);
					if (mOnUnprocessedFinishListener!=null) {
						mOnUnprocessedFinishListener.onUnprocessedFinish(STATUS_SUCCESS,list,list.size());
				    }
				} else {
					if (mOnUnprocessedFinishListener!=null) {
						mOnUnprocessedFinishListener.onUnprocessedFinish(STATUS_NULL,null,0);
				    }
					// 删除数据库
					db_unprocessed.onDelete();
				}
			}else{
				if (mOnUnprocessedFinishListener!=null) {
					mOnUnprocessedFinishListener.onUnprocessedFinish(STATUS_FAil,null,0);
			    }
				// 删除数据库
				db_unprocessed.onDelete();
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			if (mOnUnprocessedFinishListener!=null) {
				mOnUnprocessedFinishListener.onUnprocessedFinish(STATUS_FAil,null,0);
		    }
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOfficail_UnprocessedBuild).getJson();
			return new Officail_UnprocessedNet(context, json);
		}
	}
}
